﻿@{
    ViewBag.Title = "Import";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script>
    window.scriptRows = [];
    window.UserId = '@User.UserId';
    window.nrOfRows = 0;
</script>
<div id="PresentationBox" ng-app="importer">
    <div class="back-top" title="Top of Page"><div class="topArrow"></div></div>
    <h2 class="centeredHeaderText">Import loanables</h2>

    <div id="downloadBtnsDiv">
        <span>When importing your loanables, you are required to use the template provided here:    </span>
        <div class="blueDivButton">
            <a href="~/Content/import_template.csv" target="_blank">download template</a>
        </div>
        <br />
        <span>For information on how to format your data, please read the following specification: </span>
        <div class="redDivButton">
            <a href="~/Content/Specificationforimportingloanables.pdf" target="_blank">download specification</a>
        </div>
    </div>

 
    <div id="progressbar"><div class="progress-label">Loading...</div></div>
    <br /><br />

    
    <div id="importDescription">
        <span>Hover over the colours for a description</span>
    </div>
    <div id="appControllerCheckboxes">
        <div class="greenBox" data-toggle="tooltip" title="Loanable imported with the requested identifier."></div>
        <input type="checkbox" name="showSuccess" id="chkBxSuccess" onclick="checkBoxClicked(this.id);" />
        <div class="blueBox" data-toggle="tooltip" title="Loanable imported with a new identifier."></div>
        <input type="checkbox" name="showSuccessDoeID" id="chkBxSuccessDoeID" onclick="checkBoxClicked(this.id);" />
        <div class="yellowBox" data-toggle="tooltip" title="The data that was found doesn't match yours. Please choose which data to use."></div>
        <input type="checkbox" name="showConflict" id="chkBxConflict" onclick="checkBoxClicked(this.id);" />
        <div class="redBox" data-toggle="tooltip" title="Either no matching data was found or the import failed. Hover over the loanable to see the details."></div>
        <input type="checkbox" name="showError" id="chkBxError" onclick="checkBoxClicked(this.id);" />
    </div>
    <div id="pdfDiv">
        <select id="saveSelect" class="selectElement">
            <option value="saveDoeID">New identifiers</option>
            <option value="saveErrors">Errors</option>
            <option value="saveSuccess">Successful</option>
            <option value="saveConflicts">Conflicts</option>
        </select>
        <input type="button" id="saveIDToPDF" value="save to PDF" class="generalBtn" />
    </div>

    <div id="appControllerDiv" ng-controller="appController as appCtrl" style="">
        <div id="listHeader"><div style="float:left; min-height: 1em; width:10%"> ISBN </div> <div style="float:left; min-height: 1em; width:40%">Title</div> <div style="float:left; min-height: 1em; width:40%">Authors</div> <div style="float:left; min-height: 1em; width:10%">DoeLibS-ID</div></div>
        <div id="rowToServer" send-row-to-server ng-repeat="row in appCtrl.scriptRows" ng-init="rowCtrl.setUpData(row)" style="clear: both; overflow:auto"></div>
    </div>

    <div class="uploadDiv">
        <input type="button" value="select file to import" id="buttonForImport" class="inputButton" />
        <input type="file" name="file" id="uploadCSV" accept=".csv" /><br />
    </div>
</div>

<script src="~/Content/js/Views/Import/app.js"></script>
<script src="~/Content/js/Views/Import/jspdf.min.js"></script>

<script type="text/javascript">
    function checkBoxClicked(id) {
        var n = $("#appControllerCheckboxes :input:checked").length;
        if (n == 0) {
            $("#rowToServer div").show();
        }
        else {
            if ($(id).prop('checked') == false) {

                if (id == 'chkBxSuccess') {
                    $('.grayDiv.greenDiv:not(.blueDiv)').hide();
                }
                else if (id == 'chkBxSuccessDoeID') {
                    $('.grayDiv.blueDiv').hide();
                }
                else if (id == 'chkBxConflict') {
                    $('.grayDiv.yellowDiv').hide();
                }
                else if (id == 'chkBxError') {
                    $('.grayDiv.redDiv').hide();
                }
            }
            else {
                if ($('#chkBxSuccess').prop('checked')) {
                    if (n > 1) {
                        $('.grayDiv.greenDiv:not(.grayDiv.blueDiv)').show();
                    }
                    else {
                        $('.grayDiv.greenDiv:not(.grayDiv.blueDiv)').show();
                        $('.grayDiv:not(.greenDiv:not(.grayDiv.blueDiv))').hide();
                    }
                }
                if ($('#chkBxSuccessDoeID').prop('checked')) {
                    if (n > 1) {
                        $('.grayDiv.blueDiv').show();
                    }
                    else {
                        $('.grayDiv.blueDiv').show();
                        $('.grayDiv:not(.blueDiv)').hide();
                    }
                }
                if ($('#chkBxConflict').prop('checked')) {
                    if (n > 1) {
                        $('.grayDiv.yellowDiv').show();
                    }
                    else {
                        $('.grayDiv.yellowDiv').show();
                        $('.grayDiv:not(.yellowDiv)').hide();
                    }
                }
                if ($('#chkBxError').prop('checked')) {
                    if (n > 1) {
                        $('.grayDiv.redDiv').show();
                    }
                    else {
                        $('.grayDiv.redDiv').show();
                        $('.grayDiv:not(.redDiv)').hide();
                    }
                }
            }
        }
    }

    $('#buttonForImport').click(function () {
        $('#uploadCSV').click();
    }).show();

    $(function () {
        $('#appControllerCheckboxes').hide();
        $("#listHeader").hide();
        $("#importDescription").hide();
        $('#pdfDiv').hide();
        $('[data-toggle="tooltip"]').tooltip({
            animated: 'fade',
            placement: 'top',
        });
        
        $('#saveIDToPDF').click(function () {
            var doc = new jsPDF();
            arr = new Array();
            var yCoord = 15;
            var count = 0;
            var selected = $('#saveSelect option:selected').val();

            if (selected == 'saveDoeID') {
                $('.grayDiv.blueDiv').each(function () {
                    count += 1;
                    if (count % 12 === 0) {
                        doc.addPage();
                        yCoord = 15;
                    }

                    doc.fromHTML($(this).get(0), 15, yCoord, {
                        'width': 150
                    })
                    doc.line(15, yCoord, 200, yCoord);
                    yCoord += 22;
                });
                doc.save('DoeLibS-new_IDs.pdf');
            }
            else if (selected == 'saveErrors') {
                $('.grayDiv.redDiv').each(function () {
                    count += 1;
                    if (count % 12 === 0) {
                        doc.addPage();
                        yCoord = 15;
                    }

                    doc.fromHTML($(this).get(0), 15, yCoord, {
                        'width': 150
                    })
                    doc.line(15, yCoord, 200, yCoord);
                    yCoord += 22;
                });
                doc.save('DoeLibS-Errors.pdf');
            }
            else if (selected == 'saveSuccess') {
                $('.grayDiv.greenDiv:not(.blueDiv)').each(function () {
                    count += 1;
                    if (count % 12 === 0) {
                        doc.addPage();
                        yCoord = 15;
                    }

                    doc.fromHTML($(this).get(0), 15, yCoord, {
                        'width': 150
                    })
                    doc.line(15, yCoord, 200, yCoord);
                    yCoord += 22;
                });
                doc.save('DoeLibS-Success.pdf');
            }
            else if (selected == 'saveConflicts') {
                $('.grayDiv.yellowDiv').each(function () {
                    count += 1;
                    if (count % 12 === 0) {
                        doc.addPage();
                        yCoord = 15;
                    }

                    doc.fromHTML($(this).get(0), 15, yCoord, {
                        'width': 150
                    })
                    doc.line(15, yCoord, 200, yCoord);
                    yCoord += 22;
                });
                doc.save('DoeLibS-Conflicts.pdf');
            }
        });

        var progressLabel = $(".progress-label"),
            progressbar = $("#progressbar");
        progressbar.hide();
        progressbar.progressbar({
            value: 0,
            change: function () {
                progressLabel.text(parseFloat(progressbar.progressbar("value"), 10).toFixed(2) + "%");
                if (parseFloat(progressbar.progressbar("value"), 10).toFixed(2) == 100.00) {
                    progressLabel.text("Done!");
                    setTimeout(function () { progressbar.fadeOut("slow"); }, 2000);

                }
            }
        });

        $(window).scroll(function () {
            if ($(this).scrollTop() > 300) {
                $('.back-top').fadeIn();
            } else {
                $('.back-top').fadeOut();
            }
        });

        $('.back-top').click(function () {
            $('body,html').animate({
                scrollTop: 0
            }, 1600);
            return false;
        });

    });

    $('#uploadCSV').on('change', function (e) {
        var files = e.target.files;
        var fileSplit = files[0].name.split(".");
        var fileExt = fileSplit[fileSplit.length - 1];
        if (fileExt != "csv") {
            alert("You can only import .csv files.");
        }
        else {
            $("#downloadBtnsDiv").hide();
            $("#progressbar").show();
            $("#importDescription").show();
            $('#pdfDiv').show();
            $("#uploadDiv").hide();
            $("#buttonForImport").hide();
            $("#listHeader").show();
            $('#appControllerCheckboxes').show();
            Papa.parse(files[0], {
                worker: true,
                header: true,
                step: function (row) {
                    window.scriptRows.push(row.data);
                    window.nrOfRows = row.meta.lines;

                },
                complete: function () {
                    console.log("All done!");
                    angular.element($('#appControllerDiv')).scope().updateScriptRows();
                    angular.element($('#appControllerDiv')).scope().$apply();
                }
            });
        }
    });

    

</script>